package com.xuecheng.manager_course.service;

import com.xuecheng.framework.domain.course.CourseBase;
import com.xuecheng.framework.domain.course.Teachplan;
import com.xuecheng.framework.domain.course.TeachplanNode;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manager_course.dao.CourseBaseRepository;
import com.xuecheng.manager_course.dao.CourseTeachplanMapper;
import com.xuecheng.manager_course.dao.CourseTeachplanRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class CourseTeachplanService {

    @Autowired
    private CourseTeachplanMapper courseTeachplanMapper;
    @Autowired
    private CourseTeachplanRepository courseTeachplanRepository;
    @Autowired
    private CourseBaseRepository courseBaseRepository;

    public TeachplanNode findTeachplanList(String courseId){

        TeachplanNode list= courseTeachplanMapper.selectList(courseId);
        return list;

    }
    @Transactional
    public ResponseResult addTeachplan(Teachplan teachplan){
        if(teachplan==null|| StringUtils.isEmpty(teachplan.getCourseid())||StringUtils.isEmpty(teachplan.getPname())){
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        String parentid = teachplan.getParentid();
        if( parentid==null||parentid==""){
            //说明是一个新课程，只有课程名称，
            // 1,并没有赋值，连跟节点都没有
            // 2.有根节点0，但是是要创建2级节点
            parentid=this.getTeachplanRoot(teachplan.getCourseid());
       }
        Optional<Teachplan> optional = courseTeachplanRepository.findById(parentid);
        if (!optional.isPresent()){
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        Teachplan teachplanParent = optional.get();
        String grade = teachplanParent.getGrade();

        //对课程修改
        teachplan.setParentid(parentid);//存在上述新课程没有parentid的情况
        teachplan.setStatus("0");
        if (grade.equals("1")){
            teachplan.setGrade("2");
        }else if(grade.equals("2")) {
            teachplan.setGrade("3");
        }
        teachplan.setCourseid(teachplanParent.getCourseid());
        courseTeachplanRepository.save(teachplan);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    private String getTeachplanRoot(String courseid) {
       //校验课程是否存在
        Optional<CourseBase> courseBaseOptional = courseBaseRepository.findById(courseid);
        if(!courseBaseOptional.isPresent()){
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        CourseBase courseBase = courseBaseOptional.get();
        List<Teachplan> teachplans = courseTeachplanRepository.findByParentidAndAndCourseid("0", courseid);
        if (teachplans.isEmpty()){
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
            Teachplan teachplan=new Teachplan();
            teachplan.setCourseid(courseid);
            teachplan.setStatus("0");
            teachplan.setPname(courseBase.getName());
            teachplan.setParentid("0");
            teachplan.setGrade("1");
            courseTeachplanRepository.save(teachplan);
            teachplans.add(teachplan);
        }
        Teachplan teachplan = teachplans.get(0);
        return teachplan.getParentid();


    }

}
